//
//  ChannelSelectView.m
//  ZYYD-Iphone
//
//  Created by yuzhou on 12-9-21.
//  Copyright (c) 2012年 yuzhou. All rights reserved.
//

#import "ChannelSelectView.h"
#import "DataRequest.h"
#import <QuartzCore/QuartzCore.h>

@implementation ChannelSelectView
@synthesize categoryArray;

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        // Initialization code
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gotCategory:) name:gotCategoryNotify object:nil];
    }
    return self;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    
//    [self updateDisplay];
    
    UITapGestureRecognizer *tap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)] autorelease];
    [backView addGestureRecognizer:tap];
    
//    [self popAnimate];
    
    leftHarfPopImageView.image = [[UIImage imageNamed:@"leftHarfPop"] resizableImageWithCapInsets:UIEdgeInsetsMake(28, 9, 9, 19)];
    rightHarfPopImageView.image = [[UIImage imageNamed:@"rightHarfPop"] resizableImageWithCapInsets:UIEdgeInsetsMake(28, 19, 9, 9)];
    
    fillImageView.layer.cornerRadius = 5;
    fillImageView.layer.masksToBounds = YES;
    
    
}

- (void)updateDisplay
{
    self.categoryArray = [[DataRequest sharedRequest] categoryArray];
    if (categoryArray) {
        [self drawButtons];
        
        popBackView.alpha = 0;
        //    [UIView animateWithDuration:0.3 animations:^{
        //        popBackView.alpha = 1;
        //    } completion:^(BOOL finished) {
        //
        //    }];
        [UIView animateWithDuration:0.3 animations:^{
            popBackView.alpha = 1;
            CGRect rect = popBackView.frame;
            rect.size.height = 200;
            popBackView.frame = rect;
            backView.alpha = 0.35;
        }];
    }
    else {
        [[DataRequest sharedRequest] getCategory];
    }
}

- (void)popAnimate
{
    CGAffineTransform transform = contentView.transform;
    contentView.transform = CGAffineTransformMakeScale(0.8, 0.8);
    contentView.alpha = 0;
    [UIView animateWithDuration:0.1 animations:^{
        contentView.transform = CGAffineTransformMakeScale(1.1, 1.1);
        contentView.alpha = 1;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.1 animations:^{
            contentView.transform = CGAffineTransformMakeScale(0.9, 0.9);
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.1 animations:^{
                contentView.transform = transform;
            } completion:^(BOOL finished) {
                [categoryScrollView flashScrollIndicators];
            }];
        }];
    }];
}

- (void)gotCategory:(NSNotification *)notification
{
    [self updateDisplay];
}

- (void)drawButtons
{
    for (UIView *subView in categoryScrollView.subviews) {
        [subView removeFromSuperview];
    }
    categoryScrollView.contentSize = CGSizeMake(186, ceilf(categoryArray.count/2.0)*35+2);
    for (NSDictionary *dic in categoryArray) {
        int index = [categoryArray indexOfObject:dic];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = index;
        [button setTitle:[dic valueForKey:@"class_name"] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor colorWithRed:99.0/255 green:115.0/255 blue:129.0/255 alpha:1] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor colorWithRed:54.0/255 green:70.0/255 blue:85.0/255 alpha:1] forState:UIControlStateHighlighted];
        [button setTitleColor:[UIColor colorWithRed:54.0/255 green:70.0/255 blue:85.0/255 alpha:1] forState:UIControlStateSelected];
        button.titleLabel.font = [UIFont boldSystemFontOfSize:15];
        [button setBackgroundImage:[UIImage imageNamed:@"alphaButton.png"] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:@"alphaButton_highlighted.png"] forState:UIControlStateHighlighted];
        
        int row = index/2;
        int volume = index%2;
        button.frame = CGRectMake(volume==0?5:94, row*35+1, 85, 35);
        [categoryScrollView addSubview:button];
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        
        NSDictionary *currentCategory = [[NSUserDefaults standardUserDefaults] objectForKey:kCurrentCategory];
        if (currentCategory && [currentCategory isKindOfClass:[NSDictionary class]] && [[currentCategory objectForKey:@"class_id"] isEqualToString:[dic valueForKey:@"class_id"]]) {
            button.selected = YES;
            button.titleLabel.shadowOffset = CGSizeMake(0, 1);
            button.titleLabel.shadowColor = [UIColor whiteColor];
        }
    }
}

- (void)buttonAction:(UIButton *)sender
{
    int index = sender.tag;
    [[NSNotificationCenter defaultCenter] postNotificationName:kShowChannel object:[categoryArray objectAtIndex:index]];
    [self dismiss];
}

- (void)dismiss
{
    [UIView animateWithDuration:0.3 animations:^{
        CGRect rect = popBackView.frame;
        rect.size.height = 112;
        popBackView.frame = rect;
        popBackView.alpha = 0;
        backView.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [backView release];
    [categoryArray release];
    [categoryScrollView release];
    [contentView release];
    [popBackView release];
    [leftHarfPopImageView release];
    [rightHarfPopImageView release];
    [fillImageView release];
    [super dealloc];
}
@end
